import requests
import user_agent_get
import execjs


word=input("输入要翻译的单词: ")
url='https://fanyi.youdao.com/translate_o?smartresult=dict&smartresult=rule'

node=execjs.get()
ctx=node.compile(open("./youdao.js",encoding='utf-8').read())
funName="getall('{}')".format(word)
resultDict=ctx.eval(funName)
ts=resultDict['ts']
bv=resultDict['bv']
salt=resultDict['salt']
sign=resultDict['sign']

# 定义请求头字典
headers={
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Content-Length': '236',
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
'Cookie': 'OUTFOX_SEARCH_USER_ID_NCOO=1790867455.6004605; OUTFOX_SEARCH_USER_ID="-1867745431@10.169.0.82"; _ga=GA1.2.1501213222.1632645002; JSESSIONID=aaaZ5OlNBW4EZ85x6EM1x; ___rl__test__cookies=1638101928150',
'Host': 'fanyi.youdao.com',
'Origin': 'https://fanyi.youdao.com',
'Referer': 'https://fanyi.youdao.com/',
'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="96", "Google Chrome";v="96"',
'sec-ch-ua-mobile': '?0',
'sec-ch-ua-platform': '"Windows"',
'Sec-Fetch-Dest': 'empty',
'Sec-Fetch-Mode': 'cors',
'Sec-Fetch-Site': 'same-origin',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36',
'X-Requested-With': 'XMLHttpRequest'
}
data={
'i': '{}'.format(word),
'from': 'AUTO',
'to': 'AUTO',
'smartresult': 'dict',
'client': 'fanyideskweb',
'salt': '{}'.format(salt),
'sign': '{}'.format(sign),
'lts': '{}'.format(ts),
'bv': '{}'.format(bv),
'doctype': 'json',
'version': '2.1',
'keyfrom': 'fanyi.web',
'action': 'FY_BY_REALTlME',
}

response=requests.post(url=url,headers=headers,data=data).json()
print(response)
print(','.join(response['smartResult']['entries']))